var user_cart_ops={
	init:function(){
		this.eventBind();
		this.count_total();
	},
	eventBind:function(){
		var that=this;
		//点击删除事件
		$(".delC_icon").click(function(){
			var book_id=$(this).attr('data-book_id');
			$.ajax({
				url:common_ops.buildMUrl('/product/cart'),
				type:"POST",
				dataType:"json",
				data:{
					act:'del',
					book_id:book_id,
					
				},
				success:function(res){
					
					callback=null;
					if(res.code==200){
						callback=function(){
							window.location.href=common_ops.buildMUrl('/user/cart');
						}
					}
					common_ops.alert(res.msg,callback);
				},
			});
		});
		//加减效果 start
        $(".quantity-form .icon_lower").click(function () {
            var num = parseInt($(this).next(".input_quantity").val());
            that.count_total();
            if (num > 1) {
                $(this).next(".input_quantity").val(num - 1);
            	var book_id=$(this).attr('data-book_id');
            	var quantity=$(this).next().val();
            	$.ajax({
					url:common_ops.buildMUrl('/user/cart'),
					type:"POST",
					dataType:"json",
					data:{
						book_id:book_id,
						quantity:quantity,

					},
					success:function(res){
						
						callback=null;
						if(res.code==200){
							callback=function(){
								console.log(res.msg);
							}
						}
					},
				});
            }
        });
        $(".quantity-form .icon_plus").click(function () {
            var num = parseInt($(this).prev(".input_quantity").val());
            var max = parseInt($(this).prev(".input_quantity").attr("max"));
            if (num < max) {
                $(this).prev(".input_quantity").val(num + 1);
            	that.count_total();
            	
            //发送请求给后台进行商品的增加减少
            	var book_id=$(this).attr('data-book_id');
            	var quantity=$(this).prev().val();
            	$.ajax({
					url:common_ops.buildMUrl('/user/cart'),
					type:"POST",
					dataType:"json",
					data:{
						book_id:book_id,
						quantity:quantity,

					},
					success:function(res){
						
						callback=null;
						if(res.code==200){
							callback=function(){
								console.log(res.msg);
							}
						}
					},
				});
            }
        });
        //加减效果 end

        //点击结算事件
        $(".billing_btn").click(function(){
        	
        	that.sum_total();
        });
        
	},
	//计算总价
	count_total:function(){
		
        var total=0;
        var count_total=$("li").each(function(){
        	console.log('1123');
        	var price=parseInt($(this).attr('data-price')) ;
        	var quantity=parseInt($(this).find("input[name=quantity]").val()); 
        	total+=price*quantity;
        });
        console.log(total);
        $("#price").text(total);
	},
	//结算功能
	sum_total:function(){
		var product_item=[];
		$("li").each(function(){
			var book_id=$(this).find(".icon_lower").attr('data-book_id');
			var quantity=$(this).find("input[name=quantity]").val();
			product_item.push(book_id+'#'+quantity);
		});
		$.ajax({
			url:common_ops.buildMUrl('/product/order'),
			type:"post",
			dataType:"json",
			data:{
				product_item:product_item,
				sc:'cart',
			},
			success:function(res){
				callback=null;
				if(res.code==200){
					callback=function(){
						window.location.href=res.data.url;
					}
				}
				common_ops.alert(res.msg,callback);
			},
		});

	},
};
$(document).ready(function(){
	user_cart_ops.init();
});